
/**
 * 提示消息
 * @param content
 */
function tips(content) {
    var date = (new Date().getTime());
    var str = `<div id="` + date + `" style="z-index: 19999999;text-align: center;position: fixed;margin: auto;padding: 10px;background-color: black;opacity: 0.5;color: white">` + content + `</div>`
    $("body").append(str);
    var elm = $("#" + date);
    var w = elm.width();
    var h = elm.height();
    elm.css({
        width: w,
        height: h,
        left: 0,
        right: 0,
        top: 0,
        bottom: 0
    })
    setTimeout(function () {
        elm.remove();
    }, 1500)
}
/**
 * 打开页面
 */
function openHtml(t, jump, h = '350px', w = '500px', succ, type = 1, skin = 'base-skin') {
    $.get(jump, function (res) {
        var rx = /<body[^>]*>([\s\S]+?)<\/body>/i;
        var m = rx.exec(res);
        if (m) m = m[1];
        layer.closeAll();
        layer.open({
            title: t,
            type: type,
            content: m,//type==1?document.querySelector(jump).innerHTML:jump,
            shadeClose: true,
            area: [w, h],
            skin: skin,
            success: function () {
                if (succ) {
                    succ();
                }
            }
        })
    })
}
function submit(elm = '*', succ, err, before) {
    var form = layui.form;
    form.on("submit(" + elm + ")", function (d) {
        if (before) {
            var s = before(d)
            if (s === false) {
                return false;
            }
        }
        var ui = $(d.form).attr("action");
        var ty = $(d.form).attr("method");
        $.ajax({
            type: ty,
            dataType: 'json',
            data: d.field,
            url: ui,
            success: function (su) {
                layer.msg(su.msg, {
                    time: 800,
                    end: function () {
                        if (su.code == 1) {
                            if (window.frames.length != parent.frames.length) {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        }
                        if (succ) {
                            succ(su, d.field)
                        }
                    }
                })
            },
            error: function (er) {
                if (er.status != 0) {
                    tips("网络错误~!")
                }
                if (err) err(er);
            }
        });
        return false;
    })
}
/**
 * 客服
 * @param t
 * @param elm
 * @param h
 * @param w
 * @param type
 * @param skin
 */
function kefu(t,elm, h = '350px', w = '500px', type = 1, skin = 'base-skin') {
    layer.open({
        title: t,
        type: type,
        content:document.querySelector(elm).innerHTML,
        shadeClose: true,
        area: [w, h],
        skin: skin
    })
}
/**
 * 返回顶部
 */
function backTop() {
    var scrTop = document.documentElement.scrollTop || document.body.scrollTop;
    if (scrTop !== 0) {
        document.documentElement.scrollTop = 0;
        document.body.scrollTop = 0;
    } else {
        layer.msg("你已经是在最顶部了")
    }
}
function save_nickname(url) {
    layer.prompt({
        formType: 2,
        value: '',
        title: '请输入昵称',
    }, function (value, index, elem) {
        $.ajax({
            url: url,
            data: {
                nickname: value
            },
            dataType: 'json',
            type: 'post',
            success: function (res) {
                layer.msg(res.msg, {
                    end: function () {
                        if (res.code == 1) {
                            layer.close(index);
                        }
                    }
                })
            }
        })
    });
}
function save_sign(url) {
    layer.prompt({
        formType: 2,
        value: '',
        title: '请输入签名',
    }, function (value, index, elem) {
        $.ajax({
            url: url,
            data: {
                sign_name: value
            },
            dataType: 'json',
            type: 'post',
            success: function (res) {
                layer.msg(res.msg, {
                    end: function () {
                        if (res.code == 1) {
                            layer.close(index);
                        }
                    }
                })
            }
        })
    });
}
function vaildate_email(url, email_code_url) {
    var str = `<form class="layui-form" method="post" action="` + url + `">
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                  <input type="email" name="email" id="usernameInput" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                </div>
              </div>
               <div class="layui-form-item">
                <label class="layui-form-label">邮箱验证码</label>
                <div class="layui-input-inline">
                  <input type="text" name="email_code"  placeholder="请输入邮箱验证码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="padding: 0!important;">
                    <button type="button" class="layui-btn" id="vail_code_btn" >获取验证码</button>
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="vaildateEmail">立即提交</button>
                </div>
              </div>
         </form>`
    layer.open({
        title: false,
        content: str,
        area: ["500px", "240px"],
        btn: false,
        success: function (index) {
            $("#vail_code_btn").click(function () {
                var t = 60;
                var _this = $(this);
                _this.attr("disabled", true).css("background-color", "#bbbbbb");
                var timer = setInterval(function () {
                    _this.text(t + '秒后获取');
                    if (t <= 0) {
                        clearInterval(timer);
                        t = 60;
                        _this.attr("disabled", false).css("background-color", "#009688").text("获取验证码")
                    }
                    t--;
                }, 1000)
                $.ajax({
                    type: 'post',
                    url: email_code_url,
                    data: {
                        email: $("#usernameInput").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        tips(res.msg)
                    },
                    error: function (err) {
                        layer.msg("网络错误请稍后再试");
                    }
                });
            })

            submit("vaildateEmail", function (res) {
                if (res.code == 1) {
                }
            }, '', function (d) {
                var email = d.field.email;
                var code = d.field.email_code;
                if (!email) {
                    tips("请输入邮箱")
                    return false;

                }
                if (!code) {
                    tips("请输入验证码");
                    return false;
                }
            })
        }
    })
}
function out(url) {
    layer.confirm('你确定要退出系统吗?', {icon: 3, title: false}, function (index) {
        $.ajax({
            type: 'post',
            data: '',
            dataType: 'json',
            url: url,
            success: function (res) {
                layer.msg(res.msg, {
                    end: function () {
                        if (parseInt(res.code) === 1) {
                            location.href = '/'
                        }
                    }
                })
            },
            error: function (er) {
                tips("网络错误请稍后再试")
            }
        })
    });
}

/**
 * 创建文章订阅服务
 * @param url 网络请求地址
 */
function createSubscribe(url,data) {
    layer.load(2,{
        success:function(){
            $.ajax({
                url:url,
                data:data,
                type:'post',
                dataType:'json',
                error:function (erro) {
                    layer.msg("请求错误",{
                        end:function () {
                            layer.closeAll();
                        }
                    })
                },
                success:function (res) {
                    layer.msg(res.msg,{
                        end:function () {
                            layer.closeAll();
                        }
                    })
                }
            })
        }
    })
}

window.onload = function () {
    /**
     * 导航
     */
    var navIndex = localStorage.getItem("nav");
    var navElm = $(".nav .layui-layout-left .layui-nav-item");
    var commentsId = '';
    var commentStatus = false;
    var commentsPage = 1;
    switch (parseInt(navIndex)) {
        case 0:
            navIndex = 1;
            break;
    }
    navElm.eq(navIndex).addClass("layui-this").siblings(".layui-nav-item").removeClass("layui-this");
    navElm.on("click", function () {
        localStorage.setItem("nav", $(this).index());
    });
    submit('debug');
    $(document).on("click", ".rep", function () {
        var _this = $(this)
        var formElm = $(".comment-form")
        var str = formElm.clone();
        if (commentStatus == false) {
            commentsId = str.attr("id", (new Date()).getTime());
            _this.parents(".cm-right").find(".cmcontents .usrepform").html(str)
            formElm.hide();
            commentStatus = true;
        } else {
            $(commentsId).remove();
            formElm.show();
            commentStatus = false;
        }
    });
    submit('comments', function (su, d) {
        if (su.code === 1) {
            var celm = $(".comments .comments-list ul");
            var uuname = ''

            if (su.data.uuname) {
                uuname = `<span class="urp">@` + su.data.uuname + `</span>`
            }
            var str = ` <li>
                                <div class="cm-left">
                                    <a href="">
                                        <img src="` + su.data.avatar + `" class="ua" alt="">
                                    </a>
                                </div>
                                <div class="cm-right">
                                    <p class="userattr">
                                        <span class="username">` + su.data.username + `</span>
                                        <span class="date">` + su.data.create_time + `</span>
                                        <button type="button" class="rep">
                                            <i class="iconfont icon-xiaoxi"></i>
                                        </button>
                                    </p>
                                    <div class="cmcontents">
                                        <div class="cbox">  ` + uuname + d.content + `</div>
                                        <div class="usrepform"></div>
                                    </div>
                                </div>
                                <div class="clear-both"></div>
                            </li>`;

            if (commentsId) {
                $(commentsId).parents("li").after(str)
                $(commentsId).remove()
                $(".comment-form").show()
            } else {
                celm.append(str)
                $(".comment-form textarea").val('')
            }
        }
    });
    $("#loadcommentsBtn").click(function () {
        $.get($(this).data("url"), {
            page: commentsPage + 1,
        }, function (suc) {
            if (suc.code) {
                var data = suc.commentList.data;
                var from_name = '';
                var from_avatar = ''
                var touser_name = '';
                var update_time = ''
                if (data.length > 0) {
                    $.each(data, function (k, v) {

                        if (v.touser_nickname || v.touser_username) {
                            var to_name = v.touser_nickname ? v.touser_nickname : v.touser_username;
                            touser_name = `<span class="urp">@` + to_name + `</span>`
                        }
                        from_avatar = suc.file_domain + "/" + v.from_avatar;
                        from_name = v.from_nickname ? v.from_nickname : v.from_username;
                        update_time = v.update_time;

                        $(".comments .comments-list ul").append(`<li>
                                <div class="cm-left">
                                    <a href="">
                                        <img src="` + from_avatar + `" class="ua" alt="">
                                    </a>
                                </div>
                                <div class="cm-right">
                                    <p class="userattr">
                                        <span class="username">` + from_name + `</span>
                                        <span class="date">` + update_time + `</span>
                                        <button type="button" class="rep">
                                            <i class="iconfont icon-xiaoxi"></i>
                                        </button>
                                    </p>
                                    <div class="cmcontents">
                                        <div class="cbox">  ` + touser_name + v.content + `</div>
                                        <div class="usrepform"></div>
                                    </div>
                                </div>
                                <div class="clear-both"></div>
                            </li>`)
                    })
                } else {
                    tips("已经没有了")
                }
            } else {
                layer.msg("加载失败")
            }
        })
    });
    $(".collcetion").click(function () {
        var _this = $(this)
        var collection_value = _this.data("is_collection");
        $.ajax({
            type: 'post',
            url: _this.data("url"),
            dataType: 'json',
            data: {
                is_collection: collection_value,
            },
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {
                    _this.removeClass("colshow");
                    _this.siblings(".layui-btn").addClass("colshow");
                }
            },
            error: function () {
                layer.msg("网络错误请稍后再试")
            }
        })
    })
}
